package com.demo.key.util

import android.content.Context
import android.net.ConnectivityManager
import android.util.Log
import android.view.View
import android.widget.Toast
import com.demo.key.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.lang.Exception
import kotlin.math.roundToInt


fun View.show(show:Boolean){
    visibility=if (show) View.VISIBLE else View.GONE
}

fun transConnectTime(t:Long):String{
    try {
        val shi=t/3600
        val fen= (t % 3600) / 60
        val miao= (t % 3600) % 60
        val s=if (shi<10) "0${shi}" else shi
        val f=if (fen<10) "0${fen}" else fen
        val m=if (miao<10) "0${miao}" else miao
        return "${s}:${f}:${m}"
    }catch (e: Exception){}
    return "00:00:00"
}

fun getNetWorkStatus(context: Context): Int {
    val connectivityManager = context
        .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetworkInfo = connectivityManager.activeNetworkInfo
    if (activeNetworkInfo != null && activeNetworkInfo.isConnected) {
        if (activeNetworkInfo.type == ConnectivityManager.TYPE_WIFI) {
            return 2
        } else if (activeNetworkInfo.type == ConnectivityManager.TYPE_MOBILE) {
            return 0
        }
    } else {
        return 1
    }
    return 1
}

fun Context.toast(s: String){
    Toast.makeText(this, s, Toast.LENGTH_LONG).show()
}

fun serverFlagRes(coun:String)=when(coun){
    "Canada"-> R.drawable.flag_canada
    "Japan"-> R.drawable.flag_japan
    else-> R.drawable.flag_fast
}


suspend fun getDelayByServerHost(ip: String): Int {
    var delay = Int.MAX_VALUE
    var timeout = 1
    val cmd = "/system/bin/ping -w $timeout $ip"
    return withContext(Dispatchers.IO) {
        val r = ping(cmd)
        if (r != null) {
            try {
                val index: Int = r.indexOf("min/avg/max/mdev")
                if (index != -1) {
                    val tempInfo: String = r.substring(index + 19)
                    val temps = tempInfo.split("/".toRegex()).toTypedArray()
                    delay = temps[0].toFloat().roundToInt()//min
                }
            } catch (e: Exception) {

            }
        }
        delay
    }
}

private fun ping(cmd: String): String? {
    var process: Process? = null
    try {
        process = Runtime.getRuntime().exec(cmd) //执行ping指令
        val inputStream = process!!.inputStream
        val reader = BufferedReader(InputStreamReader(inputStream))
        val sb = StringBuilder()
        var line: String?
        while (null != reader.readLine().also { line = it }) {
            sb.append(line)
            sb.append("\n")
        }
        reader.close()
        inputStream.close()
        return sb.toString()
    } catch (e: IOException) {
        e.printStackTrace()
    } finally {
        process?.destroy()
    }
    return null
}

fun logQwer(string: String){
    Log.e("qwer",string)
}
